
const marked = require('marked');
const katex = require('marked-katex');
export const renderer = new marked.Renderer();
function mathsExpression(expr) {
    if (expr.match(/^\$\$[\s\S]*\$\$$/)) {
        expr = expr.substr(2, expr.length - 4);
        return katex.renderToString(expr, { displayMode: true });
    } else if (expr.match(/^\$[\s\S]*\$$/)) {
        expr = expr.substr(1, expr.length - 2);
        return katex.renderToString(expr, { isplayMode: false });
    }
}

const rendererCode = renderer.code;
renderer.code = function(code, lang, escaped) {
    if (!lang) {
        const math = mathsExpression(code);
        if (math) {
            return math;
        }
    }

    return rendererCode(code, lang, escaped);
};

const rendererCodespan = renderer.codespan;
renderer.codespan = function(text) {
    const math = mathsExpression(text);

    if (math) {
        return math;
    }

    return rendererCodespan(text);
};

